/*© 2010-2011 mOcean Mobile. A subsidiary of Mojiva, Inc. All Rights Reserved.*/
package com.adserver.adview.examples;

import com.adserver.adview.AdServerException;
import com.adserver.adview.AdServerInterstitialView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

class SampleStarter {
    public static void addComponentsToPane(Container pane) {
        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

        JButton button = addAButton("AdServer sample", pane);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    AdServerSample5 adServerSample = new AdServerSample5();
                    adServerSample.initAdServerView();
                    adServerSample.runSample();
                } catch (AdServerException e1) {
                }
            }
        });

        button = addAButton("AdServerInterstitialView sample", pane);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    final int width = 320;
                    final int height = 460;
                    AdServerInterstitialView adServerInterstitialView = new AdServerInterstitialView("8061", "16112");
                    adServerInterstitialView.setAdvertiserId("23DR67");
                    adServerInterstitialView.setGroupCode("TR57");
                    adServerInterstitialView.setMinSizeX(width);
                    adServerInterstitialView.setMinSizeY(height);
                    adServerInterstitialView.setMaxSizeX(width);
                    adServerInterstitialView.setMaxSizeY(height);
                    adServerInterstitialView.setFormDimension(new Dimension(width, height));
                    adServerInterstitialView.setAutoCloseInterstitialTime(10);
                    adServerInterstitialView.setCloseButtonShowTime(3);
                    adServerInterstitialView.setCloseButtonTransparency(200);
                    adServerInterstitialView.setCloseButtonText("Close Ads");
                    adServerInterstitialView.setCloseButtonBackgroundColor(Color.GREEN);
                    adServerInterstitialView.setCloseButtonTextColor(Color.black);
                    adServerInterstitialView.setCloseButtonLocation(new Point(20, 40));
                    adServerInterstitialView.open();
                } catch (AdServerException e1) {
                } catch (IOException e1) {
                }
            }
        });

        button = addAButton("AdServer sample with test mode", pane);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    AdServerSample4 adServerSample = new AdServerSample4();
                    adServerSample.initAdServerView();
                    adServerSample.runSample();
                } catch (AdServerException e1) {
                }
            }
        });
    }

    private static JButton addAButton(String text, Container container) {
        JButton button = new JButton(text);
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        container.add(button);
        return button;
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Ad Server sample starter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane.
        addComponentsToPane(frame.getContentPane());

        Image icon = Toolkit.getDefaultToolkit().getImage(
                SampleStarter.class.getClassLoader().getResource("images/mOcean.gif"));
        frame.setIconImage(icon);

        //Display the window.
        frame.pack();
        frame.setVisible(true);

        // Get the size of the screen
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        // Determine the new location of the window
        int w = frame.getSize().width;
        int h = frame.getSize().height;
        int x = (dim.width - w) / 2;
        int y = (dim.height - h) / 2;
        // Move the window
        frame.setLocation(x, y);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}